<template>
	<view class="indexs">
		<view class="header header_wi" :style="'padding-top: ' + navH + 'px;'">
			<view class="header_height" :style="'height:' + gaodu + 'px;line-height:' + gaodu + 'px;top:' + gaodutops + 'px;'">
				<view class="index_h_cont3">
					<view class="index_wenben_img" @tap="jumpzuo"><image :src="imgUrl + '/dzx_img33.png'" mode="widthFix"></image></view>
					<view class="index_wenben">{{ words.shop.title.value }}</view>
				</view>
			</view>
			<view class="coloe_title">s</view>
		</view>
		<view class="index_center" :style="'padding-top:' + (navH + 50) + 'px;padding-bottom: 130rpx;'">
			<view class="cangkuyuan_saomaruku_center">
				<view class="cangkuyuan_saomaruku_item2">
					<view class="cangkuyuan_saomaruku_item1_cont">
						<view class="cangkuyuan_saomaruku_item1_cont_l">
							<image :src="imgUrl + '/dzx_img187.png'"></image>
							<text>{{ $t('page_my_shop.Warehouse_name') }}</text>
						</view>
						<view class="cangkuyuan_saomaruku_item2_r3">
							<input type="text" @input="shopnameFun" :placeholder="$t('page_my_shop.placeholder1')" :value="detail.shop_name" />
						</view>
					</view>
					<view class="cangkuyuan_saomaruku_item1_cont">
						<view class="cangkuyuan_saomaruku_item1_cont_l">
							<image :src="imgUrl + '/dzx_img188.png'"></image>
							<text>{{ $t('page_my_shop.Contact_person') }}</text>
						</view>
						<view class="cangkuyuan_saomaruku_item2_r3">
							<input type="text" @input="linkmanFun" :placeholder="$t('page_my_shop.placeholder2')" :value="detail.linkman" />
						</view>
					</view>
					<view class="cangkuyuan_saomaruku_item1_cont">
						<view class="cangkuyuan_saomaruku_item1_cont_l">
							<image :src="imgUrl + '/dzx_img186.png'"></image>
							<text>{{ $t('page_my_shop.Contact_number') }}</text>
						</view>
						<view class="cangkuyuan_saomaruku_item2_r3">
							<input type="text" @input="phoneFun" :placeholder="$t('page_my_shop.placeholder3')" :value="detail.phone" />
						</view>
					</view>
					<view class="cangkuyuan_saomaruku_item1_cont">
						<view class="cangkuyuan_saomaruku_item1_cont_l">
							<image :src="imgUrl + '/dzx_img50.png'"></image>
							<text>{{ $t('page_my_shop.Business_hours') }}</text>
						</view>
						<view class="cangkuyuan_saomaruku_item2_r3">
							<input type="text" @input="shophoursFun" :placeholder="$t('page_my_shop.placeholder4')" :value="detail.shop_hours" />
						</view>
					</view>
					<view class="cangkuyuan_saomaruku_item1_cont">
						<view class="cangkuyuan_saomaruku_item1_cont_l">
							<image :src="imgUrl + '/dzx_img189.png'"></image>
							<text>{{ $t('page_my_shop.Warehouse_zip_code') }}</text>
						</view>
						<view class="cangkuyuan_saomaruku_item2_r3">
							<input type="text" @input="postFun" :placeholder="$t('page_my_shop.placeholder5')" :value="detail.post" />
						</view>
					</view>
					<view class="cangkuyuan_saomaruku_item1_cont">
						<view class="cangkuyuan_saomaruku_item1_cont_l">
							<image :src="imgUrl + '/dzx_img23.png'"></image>
							<text>{{ $t('page_my_shop.Full_address') }}</text>
						</view>
						<view class="cangkuyuan_saomaruku_item2_r">
							<input type="text" :placeholder="$t('page_my_shop.placeholder6')" @input="addressFun" :data-type="type" :value="address" />
							<view class="cangkuyuan_saomaruku_item2_r1" @tap="getchooseLocation" :data-type="type">
								<image :src="imgUrl + '/dzx_img196.png'"></image>
								<text>{{ $t('page_my_shop.positioning') }}</text>
							</view>
						</view>
					</view>
					<view class="cangkuyuan_saomaruku_item1_cont2">
						<view class="cangkuyuan_saomaruku_item1_cont_l">
							<image :src="imgUrl + '/dzx_img30.png'"></image>
							<text>{{ $t('page_my_shop.Warehouse_introduction') }}</text>
						</view>
						<view class="baoguo_item2_cont_txt">
							<textarea @input="summaryFun" :placeholder="$t('page_my_shop.placeholder7')" name="" id="" cols="30" rows="10" :value="detail.summary"></textarea>
						</view>
					</view>
					<view class="cangkuyuan_saomaruku_item1_cont2" style="border-bottom: none">
						<view class="cangkuyuan_saomaruku_item1_cont_l">
							<image :src="imgUrl + '/dzx_img192.png'"></image>
							<text>{{ $t('page_my_shop.Warehouse_door') }}</text>
						</view>
						<view class="cangkuyuan_saomaruku_item2_r5" @click="toSelectImage()">
							<view class="cangkuyuan_saomaruku_item2_r5_img"><image :src="imageSrc"></image></view>
							<view class="cangkuyuan_saomaruku_item2_r5_img"><image :src="imgUrl + '/dzx_img193.png'"></image></view>
						</view>
					</view>
				</view>
				<view class="baoguo_btns">
					<view class="baoguo_btns_posi">
						<view class="baoguo_btn" @tap="saveShop">{{ $t('page_my_shop.save') }}</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
// pages/shouye/shouye_zkc/shouye_zkc.js
const app = getApp();

let request = require('../../../utils/require');

export default {
	data() {
		return {
			navH: app.globalData.navHeight,
			gaodu: app.globalData.height,
			gaodutops: app.globalData.top,
			imgUrl: app.globalData.imgUrl,
			setting: uni.getStorageSync('setting'),
			imageSrc: '',
			imageIds: '',
			words: '',
			detail: '',
			shop_name: '',
			linkman: '',
			phone: '',
			shop_hours: '',
			post: '',
			address: '',
			summary: '',
			longitude: '',
			latitude: ''
		};
	},
	/**
	 * 生命周期函数--监听页面加载
	 */ onLoad(options) {
		var that = this;
		that.getShopDetail();
	},
	onShow() {
		var that = this;
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {},
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {},
	methods: {
		getchooseLocation() {
			var that = this;
			uni.chooseLocation({
				success: function(res) {
					that.address= res.address
					that.longitude= res.latitude
					that.latitude= res.longitude
				}
			});
		},
		shopnameFun(e) {
			var that = this;
			that.shop_name= e.detail.value
		},
		linkmanFun(e) {
			var that = this;
			that.linkman= e.detail.value
		},
		phoneFun(e) {
			var that = this;
			that.phone= e.detail.value
		},
		shophoursFun(e) {
			var that = this;
			that.shop_hours= e.detail.value
		},
		postFun(e) {
			var that = this;
			that.post= e.detail.value
		},
		addressFun(e) {
			var that = this;
			that.address= e.detail.value
		},
		summaryFun(e) {
			var that = this;
			that.summary= e.detail.value
		},
		//获取仓库信息
		getShopDetail() {
			var that = this;
			var data = {
				token: uni.getStorageSync('token')
			};
			request.get(`index.php?s=/api/shop.worker/getShopDetail&wxapp_id=` + uni.getStorageSync('web_id'), data).then(res => {
				if (res.data.code == 1) {
					if (res.data.data.shopDetail.logo_image_id != 0) {
						this.imageSrc= res.data.data.shopDetail.logo.file_path
						this.words= res.data.data.words
						this.detail= res.data.data.shopDetail
						this.address= res.data.data.shopDetail.address
					} else {
						this.words= res.data.data.words
						this.detail= res.data.data.shopDetail
						this.address= res.data.data.shopDetail.address
					}
				}
			});
		},
		jumpzuo() {
			uni.navigateBack();
		},

		toSelectImage() {
			var that = this;
			uni.chooseImage({
				count: 1, // 最多可以选择的图片张数，默认9
				sizeType: ['original', 'compressed'], //original 原图，compressed 压缩图，默认二者都有
				sourceType: ['album', 'camera'], //album 从相册选图，camera 使用相机，默认二者都有。如需直接开相机或直接选相册，请只使用一个选项
				success: function(res) {
					//判断图片格式
					uni.showLoading({
						title: '上传中...'
					});
					for (var i = 0; i < res.tempFilePaths.length; i++) {
						let tempStr = res.tempFilePaths[i].split('.');
						let lowerStr = tempStr[1].toLowerCase();

						if (lowerStr != 'png' && lowerStr !== 'jpg' && lowerStr !== 'jpeg') {
							uni.showToast({
								title: '请上传PNG、JPG、JPEG格式的图片',
								icon: 'none',
								duration: 3000
							});
							return;
						}
						console.log(res.tempFiles, 'beforre--------');
						if (res.tempFiles[0]['size'] > 2 * 1024 * 1024) {
							uni.showToast({
								title: '图片大小不能超过20M',
								icon: 'none',
								duration: 3000
							});
							return;
						}

						if (res.tempFiles[0]['size'] < 5 * 1024 * 1024) {
							//图片小于5M不压缩，大于5M压缩
							that.uploadImgFile(res.tempFilePaths[0], that);
						} else {
							uni.compressImage({
								src: res.tempFilePaths[0],
								quality: 80,
								success: res => {
									console.log(res, '=========res');
									that.uploadImgFile(res.tempFilePath, that);
								}
							});
						}
					}
				}
			});
		},
		uploadImgFile(filePath) {
			var that = this;
			uni.uploadFile({
				url: app.globalData['url'] + '/index.php?s=/api/upload/image&wxapp_id=' + uni.getStorageSync('web_id') + '&token=' + uni.getStorageSync('token'),
				filePath: filePath,
				name: 'iFile',
				formData: {
					file: filePath
				},
				success: response => {
					let res = JSON.parse(response.data);
					uni.hideLoading();
					if (res.code == 1) {
						that.imageIds= res.data.file_id
						that.imageSrc= res.data.file_path
					}
				},
				fail: err => {
					uni.hideLoading();
					console.log('请求失败_______________', err);
				}
			});
		},
		//确认入库按钮
		saveShop() {
			var that = this;
			console.log(that.detail.shop_name, 999);
			var data = {
				shop_id: that.detail.shop_id,
				logo_image_id: that.imageIds,
				shop_name: that.shop_name == '' ? that.detail.shop_name : that.shop_name,
				linkman: that.linkman == '' ? that.detail.linkman : that.linkman,
				post: that.post == '' ? that.detail.post : that.post,
				summary: that.summary == '' ? that.detail.summary : that.summary,
				phone: that.phone == '' ? that.detail.phone : that.phone,
				shop_hours: that.shop_hours == '' ? that.detail.shop_hours : that.shop_hours,
				address: that.address == '' ? that.detail.address : that.address,
				longitude: that.longitude,
				latitude: that.latitude,
				token: uni.getStorageSync('token')
			};
			request.post(`index.php?s=/api/shop.worker/saveShop&wxapp_id=` + uni.getStorageSync('web_id'), data).then(res => {
				if (res.data.code == 1) {
					uni.showToast({
						title: res.data.data
					});
					setTimeout(() => {
						uni.navigateBack({
							delta: 0
						});
					}, 1000);
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					});
				}
			});
		}
	}
};
</script>
<style>
@import './my_shop.css';
</style>
